ihelpuoo.namespace("ihelpuoo.widget.list");

var ihelpuoo_widget_droplists = null;

ihelpuoo.widget.list.hideAll = function()
{
	for(var i=0;i<ihelpuoo_widget_droplists.length;i++)
	{
		var vn = ihelpuoo_widget_droplists[i];
		var v = ihelpuoo.widget.getView(vn);
		v.hideSub();
	}
}

ihelpuoo.widget.list.DropListView = function(gasket
											,name
											,patternName
											,itemPatternName
											,moduleName
											,elementName
											,queryActionName
											,deleteActionName
											,fParseData
											,fOnChange)
{
	if(!ihelpuoo_widget_droplists)
	{
		ihelpuoo_widget_droplists = new Array();
		ihelpuoo.event.add(document.body,"click",ihelpuoo.widget.list.hideAll);
	}
	
	// Attributes ...
	this.gasket = gasket;
	this.name = name;
	this.elementName = elementName;
	this.moduleName = moduleName;
	this.queryActionName = queryActionName;
	this.deleteActionName = deleteActionName;
	this.itemPatternName = (null==itemPatternName?"eve.pattern.list.DropListItem":itemPatternName);
	
	this.view = new ihelpuoo.widget.View(this.gasket
									,(null==this.patternName?
										"eve.pattern.list.DropListShell"
										:this.patternName));
	this.fParseData = fParseData;
	this.fOnChange = fOnChange;
	
	this.oArena = null;
	this.actived = null;
	
	this.children = null;
	this.list = null;
	
	ihelpuoo.widget.registerView2(this);
	ihelpuoo_widget_droplists.push(this.name);
}

ihelpuoo.widget.list.DropListView.prototype
=
{
	/**
	 * Make current list reload data from server
	 */
	reload : function()
	{
		var url = ihelpuoo.http.server().url(this.moduleName,this.queryActionName);
		var postStr = "";
		if(this.filterString)
		{
			postStr = this.filterString;
		}
		if(ihelpuoo.isInfoEnabled())
			ihelpuoo.info(" -> " + url + (ihelpuoo.isBlank(postStr)?"":"?" +postStr));
		var doc = ihelpuoo.http.POSTXML(url,postStr);
		if(null!=doc && ihelpuoo.isObject(doc))
		{
			var nodeList = doc.documentElement.getElementsByTagName(this.elementName);
			
			if(ihelpuoo.isDebugEnabled())
				ihelpuoo.debug("Got " + nodeList.length + " items!");
			
			this.list = new Array();
			for(var i=0;i<nodeList.length;i++)
			{
				var node = nodeList.item(i);
				var data = (this.fParseData?this.fParseData(node):new ihelpuoo.widget.list.DropListItem(node));
				if(data.selected)
					this.actived = data;
				this.list.push(data);
			}
			return true;
		}
		return false;
	}
	
	/**
	 * clear sub nodes
	 */
	,
	clear : function()
	{
		if(this.oArena)
			ihelpuoo.dom.clearAllChildren(this.oArena);
	}
	
	,
	renderSub: function(itemView,item)
	{
		var v = itemView.parent;
		itemView.oTitle.innerHTML = item.text;
		itemView.oTitle.setAttribute("value",item.value);
		if(item.selected)
			itemView.active();
		if(!itemView.isEventBundled && v.fOnChange)
		{
			ihelpuoo.widget.setViewNameToElement(itemView.oTitle,itemView.parent);
			ihelpuoo.event.add(itemView.oTitle,"click",v.fOnChange);
			itemView.isEventBundled = true;
		}
	}
	
	,
	refresh : function()
	{
		if(this.reload())
		{
			this.clear();
			this.draw();
		}
	}
	
	,
	onRefresh : function()
	{
		var src = ihelpuoo.event.srcElement(this);
		var v = ihelpuoo.widget.getViewByElement(src);
		v.refresh();
	}
	
	,
	draw : function()
	{
		if(this.view.isNeverBeDrawn())
		{
			this.view.preparePattern();
			this.oBox = this.view.getElement("box");
			this.oText = this.view.getElement("text");
			this.oIcon = this.view.getElement("icon");
			this.oArena = this.view.getElement("arena");
			
			ihelpuoo.widget.addViewEvent(this,this.oText,"click",this.onShowSub);
			ihelpuoo.widget.addViewEvent(this,this.oIcon,"click",this.onShowSub);
		}
		this.view.display();	
	
		if(null==this.list || this.list.length==0 || !this.oArena)
			return;
		
		if(this.actived)
			this.view.set("text",this.actived.text);
		else
			this.view.set("text","");

		this.children = new Array();
		for(var i=0;i<this.list.length;i++)
		{
			av = new ihelpuoo.widget.list.ListItemView(this,null,"eve_list_drop_normal","eve_list_drop_active");
			av.draw(this.list[i],this.renderSub);
			this.children.push(av);
		}
	}
	
	,
	onShowSub: function()
	{
		ihelpuoo.event.cancelBubble(ihelpuoo.Event(arguments));
		ihelpuoo.widget.list.hideAll();
		var v = ihelpuoo.widget.getViewByEventSource(this);
		v.showSub();
	}
	
	,
	showSub: function()
	{
		this.oBox.className = "eve_list_drop_active";
		this.oText.className = "eve_list_drop_active";
		this.oArena.style.display="block";
	}
	
	,
	hideSub: function()
	{
		this.oBox.className = "";
		this.oText.className = "eve_list_drop_normal";
		this.oArena.style.display="none";
	}
	
	// <WEBOO:INHERIT extends="ihelpuoo.widget.View">
	,display: function(){this.view.display();}
	,getElement: function(key){return this.view.getElement(key);}
	,isNeverBeDrawn: function(){return this.view.isNeverBeDrawn();}
	,preparePattern: function(){this.view.preparePattern();}
	,hide: function(){this.view.hide();}
	,set: function(key,value){this.view.set(key,value);}
	,getString: function(key,defValue){return this.view.getString(key,defValue);}
	,getInt: function(key,defValue){return this.view.getInt(key,defValue);}
	,getBoolean: function(key,defValue){return this.view.getBoolean(key,defValue);}
	,addEvent: function(key,eventName,fnHandler,bCapture){this.view.addEvent(key,eventName,fnHandler,bCapture);}
	,removeEvent: function(key,eventName,fnHandler,bCapture){this.view.removeEvent(key,eventName,fnHandler,bCapture);}
	// </WEBOO:INHERIT>
}